package com.scalar.chiptrack.operations.dataentry.lotsplit.actioncommands;

import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.inventory.forms.InventoryDataEntryForm;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.lotsplit.forms.LotSplitForm;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.Lot;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Sep 26, 2006
 * Time: 2:34:28 AM
 */
public class LotSplitLoadLotNumbersCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/lotSplit.do" ;


    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {
        LotSplitForm lotSplitForm = (LotSplitForm) form;

        HashMap errors  = new HashMap();
        HashMap info    = new HashMap();

        String action   = request.getParameter("action");
        String psType   = null;
        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);


        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        ArrayList locationList;
        ArrayList invLocationsList    = new ArrayList();
        VendorLocation location;
        String psTypeName;

        try
        {
            psType = lotSplitForm.getProcessType().substring(0, lotSplitForm.getProcessType().indexOf("/"));
            //lotSplitForm.setPsType( psType );

            psTypeName =lotSplitForm.getProcessType().substring(lotSplitForm.getProcessType().indexOf("/") + 1, lotSplitForm.getProcessType().length() );
            if ( psTypeName.equalsIgnoreCase("MARK & PACK") )
            {
                 psTypeName = "Mark &amp; Pack";
            }
            locationList= VendorDAO.getInventoryLocations(psTypeName);

            for (int i = 0; i < locationList.size(); i++)
            {
                location = (VendorLocation) locationList.get(i);
                invLocationsList.add(new org.apache.struts.util.LabelValueBean(location.getLocationName(), location.getLocationID()));
            }

           ArrayList runningHoldLotsList =  DataEntryDAO.getLotInfoByPsType( "ALL", psTypeName, psType, Integer.parseInt(lotSplitForm.getProcessStepId()), Integer.parseInt(lotSplitForm.getLocationId() ), Integer.parseInt( lotSplitForm.getLotId() )  );

           if ( runningHoldLotsList == null || runningHoldLotsList.size() == 0 )
           {
                info.put("No "+psType+" lots available" , null );
           }

           lotSplitForm.setAllwipLotsList( runningHoldLotsList );

           request.setAttribute("processTypeList", DataEntryDAO.loadProcessList(null, "all") );
           request.setAttribute("partNumbersList", NewProductDAO.loadPartNumbers( null, 0, psType, true, true, true, 0 ) );
           request.setAttribute("locationList", invLocationsList);
           request.setAttribute("splitLotNoList", runningHoldLotsList );
           request.setAttribute("action", action);
           request.setAttribute("psType", psType);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return mapping.findForward("lotSplit");


    }
}